package com.ruoyi.mall.pms.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mall.pms.domain.PmsProduct;
import com.ruoyi.mall.pms.domain.dto.PmsProductDto;
import com.ruoyi.mall.pms.service.IPmsProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 商品Controller
 *
 * @author xuxing
 * @date 2021-02-03
 */
@RestController
@RequestMapping("/pms/product")
public class PmsProductController extends BaseController
{
    @Autowired
    private IPmsProductService pmsProductService;

    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('pms:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(PmsProduct pmsProduct)
    {
        startPage();
        List<PmsProduct> list = pmsProductService.selectPmsProductList(pmsProduct);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('pms:product:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PmsProduct pmsProduct)
    {
        List<PmsProduct> list = pmsProductService.selectPmsProductList(pmsProduct);
        ExcelUtil<PmsProduct> util = new ExcelUtil<PmsProduct>(PmsProduct.class);
        return util.exportExcel(list, "product");
    }

    /**
     * 获取商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('pms:product:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id, @RequestParam(required = false) Boolean detail) {
        return AjaxResult.success(pmsProductService.selectPmsProductById(id, detail));
    }

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('pms:product:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PmsProductDto pmsProductDto) {
        return toAjax(pmsProductService.insertPmsProduct(pmsProductDto.getProduct(), pmsProductDto.getMemberPriceList(), pmsProductDto.getLadderList(), pmsProductDto.getFullReductionList(), pmsProductDto.getAttributeValueList(), pmsProductDto.getProductSkuList(), pmsProductDto.getSubjectIds()));
    }

    /**
     * 修改商品
     */
    @PreAuthorize("@ss.hasPermi('pms:product:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PmsProductDto pmsProductDto)
    {
        return toAjax(pmsProductService.updatePmsProduct(pmsProductDto));
    }

    /**
     * 修改商品状态
     */
    @PreAuthorize("@ss.hasPermi('pms:product:edit')")
    @Log(title = "商品状态", businessType = BusinessType.UPDATE)
    @PutMapping("/status")
    public AjaxResult editStatus(@RequestBody Map<String, Integer> map) {
        return toAjax(pmsProductService.updatePmsProductStatus(map));
    }

    /**
     * 删除商品
     */
    @PreAuthorize("@ss.hasPermi('pms:product:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pmsProductService.deletePmsProductByIds(ids));
    }
}
